package com.portal.caresms;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import com.caresms.model.Category_Master;
import com.caresms.model.City_Master;
import com.caresms.model.Country_Master;
import com.caresms.model.Keyword_Master;
import com.caresms.model.Sms_Master;
import com.caresms.service.Category_MasterLocalServiceUtil;
import com.caresms.service.City_MasterLocalServiceUtil;
import com.caresms.service.Country_MasterLocalServiceUtil;
import com.caresms.service.Keyword_MasterLocalServiceUtil;
import com.caresms.service.persistence.Keyword_MasterFinderUtil;
import com.liferay.portal.SystemException;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.model.CountryModel;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

public class Caresms extends MVCPortlet {
	
	public void abc(ActionRequest actionrequest,ActionResponse actionresponce)
	{
		String keyword=actionrequest.getParameter("keyword");
		System.out.println("ssearch"+keyword);
		try {
			long keyid=Keyword_MasterLocalServiceUtil.getbykeyname(keyword);
			System.out.println("ssearch"+keyid);
			List<Sms_Master> sm=Keyword_MasterLocalServiceUtil.getBysms(keyid);
			System.out.println("sms"+sm.size());
			actionrequest.setAttribute("search", sm);
			actionrequest.setAttribute("k", keyword);
			
			actionresponce.setRenderParameter("jspPage", "/Search/view.jsp");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	public void catDisplay(ActionRequest actionrequest,ActionResponse actionresponse)
	{
		String catid=ParamUtil.getString(actionrequest, "catid");
		System.out.println("catid"+catid);
		 long l=Long.valueOf(catid);
		try {
			List<Sms_Master> sm=Keyword_MasterLocalServiceUtil.getSmsByCatid(l);
			System.out.println("size of sms"+sm.size());
			actionrequest.setAttribute("catDisplay", sm);
			actionresponse.setRenderParameter("jspPage", "/Search/CategoryDisplay.jsp");
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void catAction(ActionRequest arq,ActionResponse ars){
		String category= arq.getParameter("category");
		boolean active=ParamUtil.getBoolean(arq, "isActive");
		System.out.println("categ"+category);;
		Category_MasterLocalServiceUtil.addtodb(category,active);
		
		
	}
	public void editcategory(ActionRequest arrq,ActionResponse arrs) throws com.liferay.portal.kernel.exception.PortalException, com.liferay.portal.kernel.exception.SystemException{
		String str=arrq.getParameter("edit");
		long l=Long.valueOf(str);
		Category_Master cm = Category_MasterLocalServiceUtil.getCategory_Master(l);
		System.out.println("Cat master"+ cm);
		arrq.setAttribute("edit",cm);
		arrs.setRenderParameter("jspPage", "/category/edit.jsp");
	}
	public void updatenameURL(ActionRequest arrqq,ActionResponse arrss) throws com.liferay.portal.kernel.exception.PortalException, com.liferay.portal.kernel.exception.SystemException {

	    String str = arrqq.getParameter("category");
	    String str2 = arrqq.getParameter("edit");
	    long l=Long.valueOf(str2);
	    boolean active=ParamUtil.getBoolean(arrqq, "isActive");
	    Category_Master ctm = Category_MasterLocalServiceUtil.getCategory_Master(l);
	    ctm.setCatName(str);
	    ctm.setIsActive(active);
	    Category_MasterLocalServiceUtil.updateCategory_Master(ctm);
	    arrss.setRenderParameter("JspPage", "/category/Addcategory.jsp");    
	}
	
	
	
	public void addcountry(ActionRequest arrqq,ActionResponse arrss) throws com.liferay.portal.kernel.exception.PortalException, com.liferay.portal.kernel.exception.SystemException {
		{
			String str = arrqq.getParameter("country");
			Country_MasterLocalServiceUtil.addcountry(str);
			 arrss.setRenderParameter("JspPage", "/CountryMaster/country.jsp");
		}
	}
	public void editcountry(ActionRequest arrq,ActionResponse arrs) throws com.liferay.portal.kernel.exception.PortalException, com.liferay.portal.kernel.exception.SystemException{
		String str=arrq.getParameter("edit");
		long l=Long.valueOf(str);
		//Category_Master cm = Category_MasterLocalServiceUtil.getCategory_Master(l);
		Country_Master cm=Country_MasterLocalServiceUtil.getCountry_Master(l);
		arrq.setAttribute("editcountry",cm);
		arrs.setRenderParameter("jspPage", "/CountryMaster/editcountry.jsp");
	}
	public void savecountry(ActionRequest arrqq,ActionResponse arrss) throws com.liferay.portal.kernel.exception.PortalException, com.liferay.portal.kernel.exception.SystemException {

	    String str = arrqq.getParameter("CountryName");
	    String str2 = arrqq.getParameter("edit");
	    long l=Long.valueOf(str2);
	    
	    Country_Master cm=Country_MasterLocalServiceUtil.getCountry_Master(l);
	    cm.setCountryName(str);
	    Country_MasterLocalServiceUtil.updateCountry_Master(cm);
	    arrss.setRenderParameter("JspPage", "/CountryMaster/country.jsp");    
	}
	
	public void cityURL(ActionRequest arrqq,ActionResponse arrss) throws com.liferay.portal.kernel.exception.PortalException, com.liferay.portal.kernel.exception.SystemException {
		{
			String str=arrqq.getParameter("countryname");
			long l=Long.valueOf(str);
			String city=arrqq.getParameter("city");
			City_MasterLocalServiceUtil.addCity(l,city);
			
		}
	}
	public void editcity(ActionRequest arrq,ActionResponse arrs) throws com.liferay.portal.kernel.exception.PortalException, com.liferay.portal.kernel.exception.SystemException{
		String str=arrq.getParameter("edit");
		long l=Long.valueOf(str);
		//Category_Master cm = Category_MasterLocalServiceUtil.getCategory_Master(l);
		City_Master cm=City_MasterLocalServiceUtil.getCity_Master(l);
		arrq.setAttribute("editcity",cm);
		arrs.setRenderParameter("jspPage", "/CityMaster/editcity.jsp");
	}
	public void updatecityURL(ActionRequest arrqq,ActionResponse arrss) throws com.liferay.portal.kernel.exception.PortalException, com.liferay.portal.kernel.exception.SystemException {
		
	    String str = arrqq.getParameter("countryname");
		long l1=Long.valueOf(str);
	    String str2 = arrqq.getParameter("edit");
	    long l=Long.valueOf(str2);
	    String city=arrqq.getParameter("city");
	    City_Master cm=City_MasterLocalServiceUtil.getCity_Master(l);
	    cm.setCountryId(l1);
	    cm.setCityName(city);
	    City_MasterLocalServiceUtil.updateCity_Master(cm);
	    arrss.setRenderParameter("JspPage", "/CityMaster/City.jsp");    
	}
	
	public void keyAction(ActionRequest arrqq,ActionResponse arrss) throws com.liferay.portal.kernel.exception.PortalException, com.liferay.portal.kernel.exception.SystemException {
		 String str = arrqq.getParameter("Keyword");
		 String str2 = arrqq.getParameter("Categroy_List");
		  long l=Long.valueOf(str2);
		 System.out.println("keyword"+l);
		 boolean active = ParamUtil.getBoolean(arrqq, "isActive");
		 if(!str.equalsIgnoreCase("")){
		 List<String> list=new ArrayList<String>(Arrays.asList(str.split(",")));
		 Set<String> set=new HashSet<String>(list);
		 
		 for (String string : set) {
			 System.out.println(string.trim());
			 String keystr=string.trim();
			 Keyword_MasterLocalServiceUtil.addKey(l,keystr,active);
			 
		}
		 }
		 if(l>0)
		 {
		List<Keyword_Master> list=Keyword_MasterLocalServiceUtil.getByCatId(l);
		arrqq.setAttribute("result",list);
		 }
	}
	
		
	@SuppressWarnings("unchecked")
	public void serveResource(ResourceRequest resourceRequest,
            ResourceResponse resourceResponse) throws IOException,
            PortletException {
		        // TODO Auto-generated method stub
     String query = resourceRequest.getParameter("q");
        JSONObject json = JSONFactoryUtil.createJSONObject();
        JSONArray results = JSONFactoryUtil.createJSONArray();
      
        json.put("response", results);
        DynamicQuery dynamicQuery =null;
        String keyWord =null;
        int c=1;        
        @SuppressWarnings("unused")
		List<String> keywordList = new ArrayList<String>();
        List<Keyword_Master> myKeyWordList = null;  
        String searchPattern = query;
        searchPattern = searchPattern.replace('*', '%');        
        dynamicQuery = DynamicQueryFactoryUtil.forClass(Keyword_Master.class).add(PropertyFactoryUtil.forName("KeywordName").like("%"+searchPattern+"%" )).add(PropertyFactoryUtil.forName("field3").eq(new Integer(c)));
        try {
			myKeyWordList = (List<Keyword_Master>)Keyword_MasterLocalServiceUtil.dynamicQuery(dynamicQuery);
		} catch (com.liferay.portal.kernel.exception.SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        for (int i = 0; i < myKeyWordList.size(); i++ ) {
             JSONObject listEntry = JSONFactoryUtil.createJSONObject();
            keyWord=myKeyWordList.get(i).getKeywordName();
           
      				listEntry.put("key", keyWord);
				 results.put(listEntry);
            
           
            //System.out.println("results.put(listEntry)"+results);

        }
         java.io.PrintWriter writer = resourceResponse.getWriter();
            writer.println(json.toString());
    }

	
}
